﻿
namespace Alogient.Cameleon.Forms.Sdk.Util
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Resources;
    using System.Text;

    public class WebUtil
    {
        public static string EncodeAccents(string str)
        {
            var sb = new StringBuilder();
            foreach (char c in str)
            {
                if (c > '~')
                {
                    sb.Append("&#");
                    sb.Append(((int)c).ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
                    sb.Append(';');
                }
                else
                {
                    sb.Append(c);
                }
            }

            return sb.ToString();
        }

        private static ResourceManager rmValidators = new ResourceManager("Alogient.Cameleon.Forms.Sdk.Localized.Validators", Assembly.GetExecutingAssembly());
        private static ResourceManager rmLabels = new ResourceManager("Alogient.Cameleon.Forms.Sdk.Localized.Labels", Assembly.GetExecutingAssembly());

        internal static string GetValidatorRessource(string name)
        {
            return rmValidators.GetString(name);
        }

        internal static string GetLabelsRessource(string name)
        {
            return rmLabels.GetString(name);
        }

        internal static string GetCurrentUsername()
        {
            System.Web.Security.MembershipUser user = null;
            string username = string.Empty;

            // Check if membership is enabled
            if (MembershipEnabled())
            {
                if (MembershipMode() == System.Web.Configuration.AuthenticationMode.Forms)
                {
                    user = System.Web.Security.Membership.GetUser();
                }
                else if (MembershipMode() == System.Web.Configuration.AuthenticationMode.Windows)
                {
                    // When user Windows Authentication, GetUser() will always return a null value
                    // Instead by using the Identity Username, we can get the user informations
                    string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                    username = name.Substring(name.IndexOf('\\') + 1);
                    user = System.Web.Security.Membership.GetUser(username);
                }

                // Get user PKID and Username
                if (user != null)
                {
                    username = user.UserName;
                }
            }

            return username;
        }

        internal static object GetCurrentUserPKID()
        {
            System.Web.Security.MembershipUser user = null;

            // Check if membership is enabled
            if (MembershipEnabled())
            {
                if (MembershipMode() == System.Web.Configuration.AuthenticationMode.Forms)
                {
                    user = System.Web.Security.Membership.GetUser();
                }
                else if (MembershipMode() == System.Web.Configuration.AuthenticationMode.Windows)
                {
                    // When user Windows Authentication, GetUser() will always return a null value
                    // Instead by using the Identity Username, we can get the user informations
                    string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                    string username = name.Substring(name.IndexOf('\\') + 1);
                    user = System.Web.Security.Membership.GetUser(username);
                }

                // Get user PKID and Username
                if (user != null)
                {
                    return user.ProviderUserKey;
                }
            }

            return null;
        }

        internal static bool MembershipEnabled()
        {
            return System.Web.Security.Membership.Provider != null && System.Web.Security.Membership.Provider.Name == "CMMembershipProvider";
        }

        internal static System.Web.Configuration.AuthenticationMode MembershipMode()
        {
            // Check mode Forms or Windows
            System.Web.Configuration.AuthenticationSection section = null;

            try
            {
                section = (System.Web.Configuration.AuthenticationSection)System.Web.Configuration.WebConfigurationManager.GetSection("system.web/authentication");
            }
            catch (System.Configuration.ConfigurationErrorsException) { }
            
            return section != null ? section.Mode : System.Web.Configuration.AuthenticationMode.None;
        }
    }
}
